package org.fireking.myapplication.drawing.impl

import android.R
import android.graphics.Canvas
import android.graphics.Paint
import android.util.Log
import com.github.mikephil.charting.utils.Transformer
import org.fireking.myapplication.ChartDrawingRenderer
import org.fireking.myapplication.drawing.ShapeDrawingStrategy
import kotlin.math.abs

class RectangleDrawingStrategy(
    var left: Float, var top: Float, var right: Float, var bottom: Float
) : ShapeDrawingStrategy {
    override fun draw(canvas: Canvas, transformer: Transformer?, paint: Paint) {
        transformer ?: return
        val leftTopPoints = floatArrayOf(left, top)
        val rightBottomPoints = floatArrayOf(right, bottom)
        transformer.pointValuesToPixel(leftTopPoints)
        transformer.pointValuesToPixel(rightBottomPoints)
        canvas.drawRect(
            leftTopPoints[0], leftTopPoints[1], rightBottomPoints[0], rightBottomPoints[1], paint
        )
    }

    override fun containsPoint(x: Float, y: Float, touchTolerance: Float): Boolean {
        return x >= left - touchTolerance && x <= right + touchTolerance && y >= bottom - touchTolerance && y <= top + touchTolerance
    }

    override fun containsPointInside(x: Float, y: Float, edgeTolerance: Float): Boolean {
        val innerLeft = left + edgeTolerance
        val innerRight = right - edgeTolerance
        val innerTop = top + edgeTolerance
        val innerBottom = bottom - edgeTolerance
        return x > innerLeft && x < innerRight && y > innerTop && y < innerBottom
    }

    override fun getControlPoints(): List<ChartDrawingRenderer.ControlPoint> {
        return listOf(
            ChartDrawingRenderer.ControlPoint(
                0, left, top, ChartDrawingRenderer.ControlPointType.CORNER
            ), ChartDrawingRenderer.ControlPoint(
                1, right, top, ChartDrawingRenderer.ControlPointType.CORNER
            ), ChartDrawingRenderer.ControlPoint(
                2, right, bottom, ChartDrawingRenderer.ControlPointType.CORNER
            ), ChartDrawingRenderer.ControlPoint(
                3, left, bottom, ChartDrawingRenderer.ControlPointType.CORNER
            ), ChartDrawingRenderer.ControlPoint(
                4, (left + right) / 2, top, ChartDrawingRenderer.ControlPointType.TOP
            ), ChartDrawingRenderer.ControlPoint(
                5, right, (top + bottom) / 2, ChartDrawingRenderer.ControlPointType.RIGHT
            ), ChartDrawingRenderer.ControlPoint(
                6, (left + right) / 2, bottom, ChartDrawingRenderer.ControlPointType.BOTTOM
            ), ChartDrawingRenderer.ControlPoint(
                7, left, (top + bottom) / 2, ChartDrawingRenderer.ControlPointType.LEFT
            )
        )
    }

    override fun applyControlPointMove(controlPointId: Int, deltaX: Float, deltaY: Float) {
        when (controlPointId) {
            0 -> {
                left += deltaX
                top += deltaY
            }

            1 -> {
                right += deltaX
                top += deltaY
            }

            2 -> {
                right += deltaX
                bottom += deltaY
            }

            3 -> {
                left += deltaX
                bottom += deltaY
            }

            4 -> {
                top += deltaY
            }

            5 -> {
                right += deltaX
            }

            6 -> {
                bottom += deltaY
            }

            7 -> {
                left += deltaX
            }
        }
    }

    override fun move(deltaX: Float, deltaY: Float) {
        left += deltaX
        top += deltaY
        right += deltaX
        bottom += deltaY
    }
}